package  {
	
	import display.BasicScreen;
	import display.Intro;
	
	import events.ScreenEvent;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.system.Security;
	import flash.ui.ContextMenu;
	
	import org.ilovecode.mvc.View;
	import org.ilovecode.screen.ScreenName;
	import org.ilovecode.utils.Utilities;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class Main extends View {
		
		
		private var intro:Intro;
		private var basicScreen:BasicScreen;
		
		public function Main() {
			(this.stage) ? init() : this.addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(event:Event=null):void {
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
			Utilities.loadURL("xml/config.xml", loadConfig_Complete);
			
			/*intro = new Intro();
			intro.addEventListener(ScreenEvent.SWITCH_SCREEN, intro_SwitchScreen);
			this.addChild(intro);
			initListeners();
			initContextMenu();*/
		}
		
		override protected function construct():void
		{
			intro = new Intro();
		}
		
		override protected function initUI():void
		{
			intro.addEventListener(ScreenEvent.SWITCH_SCREEN, intro_SwitchScreen);
			this.addChild(intro);
			
			var cMenu:ContextMenu = new ContextMenu();
			cMenu.hideBuiltInItems();
			
			this.contextMenu = cMenu;
		}
		
		private function loadConfig_Complete(event:Event):void
		{
			var configXML:XML = XML(event.target.data);
			
			var screenXMLs:Array = new Array();
			for each (var item:XML in configXML.descendants("asset"))
				screenXMLs.push(String(item.@url));
			
			Utilities.loadMultipleURL(screenXMLs, loadXMLs_Complete);
		}
		
		private function loadXMLs_Complete(xmls:Array):void
		{
			model.screenXMLs = xmls;
		}
		
		/*private function initListeners():void {
			
		}*/
		
		//{ region Context menu
		
		/*private function initContextMenu():void {
			var cMenu:ContextMenu = new ContextMenu();
			cMenu.hideBuiltInItems();
			
			this.contextMenu = cMenu;
		}*/
		
		//} endregion
		
		private function intro_SwitchScreen(event:Event):void {
			intro.removeEventListener(ScreenEvent.SWITCH_SCREEN, intro_SwitchScreen);
			this.removeChild(intro);
			intro = null;
			
			basicScreen = new BasicScreen();
			basicScreen.addEventListener(ScreenEvent.SWITCH_SCREEN, basicScreen_SwitchScreen);
			this.addChild(basicScreen);
			controller.showScreen(ScreenName.START);
		}
		
		private function basicScreen_SwitchScreen(event:Event):void {
			basicScreen.removeEventListener(ScreenEvent.SWITCH_SCREEN, basicScreen_SwitchScreen);
			this.removeChild(basicScreen);
			basicScreen = null;
			
			intro = new Intro();
			intro.addEventListener(ScreenEvent.SWITCH_SCREEN, intro_SwitchScreen);
			this.addChild(intro);
		}
	}
	
}